// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.ObjectModel;
using System.Web.Security;
using System.Web.UI.WebControls;
using BlueOnionSoftware.Bloget.Properties;

namespace BlueOnionSoftware.Bloget
{
    internal class UsersView : View
    {
        private bool _disposed;
        private readonly GridView _users = new GridView();
        private readonly CreateUserWizard _createUser = new CreateUserWizard();
        private readonly UserCollection _userCollection = new UserCollection();

        internal UsersView(Bloget bloget)
            : base(bloget)
        {
        }

        protected override string RootClassId()
        {
            return "users-view";
        }

        protected override void ShowView()
        {
            if (Bloget.IsAdministrator == false)
            {
                var passwordForm = new PasswordView(Bloget);
                passwordForm.Show();
                return;
            }

            MainMenu.ShowMenu(Bloget);

            foreach (MembershipUser user in Bloget.GetAllUsers())
            {
                if (Bloget.IsUserAdministrator(user))
                {
                    _userCollection.Add(new User(user, Bloget.AdministratorRole));
                }

                else if (Bloget.IsUserAuthor(user))
                {
                    _userCollection.Add(new User(user, Bloget.AuthorRole));
                }
            }

            _createUser.CreateUserStep.Title = string.Empty;
            _createUser.LoginCreatedUser = false;
            _createUser.CreatedUser += CreatedUser;
            Bloget.Controls.Add(_createUser);

            _users.AutoGenerateColumns = false;
            _users.AutoGenerateEditButton = true;
            _users.RowEditing += UsersRowEditing;
            _users.CssClass = RootClassId() + "-users";

            var nameColumn = new BoundField {DataField = "UserName", HeaderText = Resources.UserViewColumnName};
            _users.Columns.Add(nameColumn);

            var role = new BoundField {DataField = "Role", HeaderText = Resources.UsersViewColumnRole};
            _users.Columns.Add(role);

            var emailColumn = new BoundField {DataField = "Email", HeaderText = Resources.UserViewColumnEmail};
            _users.Columns.Add(emailColumn);

            var lastLogin = new BoundField {DataField = "LastLogin", HeaderText = Resources.UserViewColumnLastLogin, ReadOnly = true};
            _users.Columns.Add(lastLogin);

            _users.DataSource = _userCollection;
            Bloget.Controls.Add(_users);
        }

        private void CreatedUser(object sender, EventArgs e)
        {
            Roles.AddUserToRole(_createUser.UserName, Bloget.AuthorRole);
        }

        private void UsersRowEditing(object sender, GridViewEditEventArgs e)
        {
            if (e != null)
            {
                var user = _userCollection[e.NewEditIndex];

                Bloget.Redirect(Bloget.BuildQueryString
                                    (Mode.EditUser, null, 0, null, null, default(DateTime), user.UserName));
            }
        }

        protected override void Dispose(bool managed)
        {
            try
            {
                if (_disposed == false)
                {
                    _disposed = true;

                    if (managed)
                    {
                        _createUser.Dispose();
                        _users.Dispose();
                    }
                }
            }

            finally
            {
                base.Dispose(managed);
            }
        }
    }

    public class User
    {
        public User(MembershipUser user, string roleArg)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (string.IsNullOrEmpty(roleArg))
            {
                throw (roleArg == null)
                    ? new ArgumentNullException("roleArg")
                    : new ArgumentException("empty", "roleArg");
            }
            UserName = user.UserName;
            Email = user.Email;
            LastLogin = user.LastLoginDate;
            Role = roleArg;
        }

        public string UserName { get; set; }
        public string Email { get; set; }
        public string Role { get; set; }
        public DateTime LastLogin { get; set; }
    }

    internal class UserCollection : Collection<User>
    {
    }
}